<?php
/**
 * Created by Veyron
 * User: Vy
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/1/17
 * Time: 11:33
 */
declare(strict_types=1);
namespace app\api\controller;
use app\common\business\User as UserBis;

class User extends AuthBase {
    public function index(){
        $user = (new UserBis())->getNormalUserById($this->userId);
        $resultUser = [
            'id'        => $this->userId,
            'username'  => $user['username'],
            'sex'       => $user['sex'],
        ];
        return show(config('status.success'),'OK',$resultUser);
    }
    public function update(){
        $username = input('username','','trim');
        $sex      = input('sex',0,'intval');
        $data = [
            'username' => $username,
            'sex'      => $sex
        ];
        $valiDate = new \app\api\validate\User();
        if(!$valiDate->scene('update')->check($data)){
            return show(config('status.error'),$valiDate->getError());
        }
        $userBisObj = new UserBis();
        $user = $userBisObj->update($this->userId,$data);

        if(!$user){
            return show(config('status.error'),'更新失败');
        }
        //用户数据更新成功 redis 内容同步更新
        $redisData = [
            'id'        => $this->userId,
            'username'  => $username
        ];
        cache(config('redis.token_pre').$this->accessToken,$redisData);
        return show(config('status.success'),'OK');
    }
}
